import { MessageBox } from 'mint-ui'
let mutations = {
  changeDetailData(state, data) {
    state.detailData = data
  },
  changeLoading(state, data) {
    state.isLoading = data
  },
  goodsList(state, data) {
    state.goodsList = data
  },
  //购物车操作
  addCarListData(state, data) {
    //判断店铺是否存在
    let isShop = state.cartListData.find(item => {
      return item.ru_id == data.ru_id
    })
    //判断商品是否存在
    let isGoods = state.cartListData.find(item => {
      return item.children.find(value => {
        return value.goods_id == data.children[0].goods_id
      })
    })
    // 店铺存在
    if (isShop) {
      // 商品存在
      if (isGoods) {
        for (let i = 0; i < isGoods.children.length; i++) {
          if (isGoods.children[i].goods_id == data.children[i].goods_id) {
            isGoods.children[i].buyNum += data.children[0].buyNum * 1
          }
        }
        // 商品不存在
      } else {
        isShop.children.push(data.children[0])
      }
      // 店铺不存在
    } else {
      state.cartListData.push(data)
    }
    localStorage.setItem("carData", JSON.stringify(state.cartListData));
  },
  //购物车加/减操作
  addCarNum(state, data) {
    for (let i = 0; i < state.cartListData.length; i++) {
      if (state.cartListData[i].ru_id == data.cartlist.ru_id) {
        state.cartListData[i].children[data.index] = data.cartlist.children[data.index];
      }
    }
    localStorage.setItem("carData", JSON.stringify(state.cartListData));
  },
  //商品点击选择
  goodSelect(state, data) {
    for (let i = 0; i < state.cartListData.length; i++) {
      if (state.cartListData[i].ru_id == data.item.ru_id) {
        state.cartListData[i].children[data.index] = data.item
        // 商品与店铺
        let flag = state.cartListData[i].children.every(item => {
          return item.isSelect == true
        })
        if (flag) {
          state.cartListData[i].isShopSelect = true
        } else {
          state.cartListData[i].isShopSelect = false
        }
        //商品与总计
        state.cartListData[i].children.map(item => {
          if (item.isSelect) {
            state.total += item.buyNum * 1
          }
        })
      }
    }
    // 商品与全选
    state.selectAll = state.cartListData.every(item => {
      return item.isShopSelect == true
    })
    localStorage.setItem("carData", JSON.stringify(state.cartListData));
  },
  //店铺点击选择
  shopSelect(state, data) {
    for (let i = 0; i < state.cartListData.length; i++) {
      if (state.cartListData[i].ru_id == data.item.ru_id) {
        state.cartListData[i] = data.item;
        //店铺与商品
        state.cartListData[i].children.map(item => {
          item.isSelect = state.cartListData[i].isShopSelect
        })
        //店铺与全选
        state.selectAll = state.cartListData.every(value => {
          return value.isShopSelect == true
        })
      }
    }
    localStorage.setItem("carData", JSON.stringify(state.cartListData));
    localStorage.setItem("selectAll", JSON.stringify(state.isSelectAll));
  },
  //全选
  selectAll(state) {
    state.selectAll = !state.selectAll
    //全选与店铺
    state.cartListData.map(value => {
      value.isShopSelect = state.selectAll
      // 全选与商品
      value.children.map(item => {
        item.isSelect = state.selectAll
      })
    })
    //总计
    state.cartListData.map(item => {
      item.children.map(value => {
        if (value.isSelect) {
          state.total += value.buyNum * 1
        } else {
          state.total = 0
        }
      })
    })
    localStorage.setItem("carData", JSON.stringify(state.cartListData));
    localStorage.setItem("selectAll", state.isSelectAll);
  },
  //删除
  delete(state, data) {
    state.cartListData.forEach((item,index) => {
      if (item.ru_id == data.item.ru_id) {
        MessageBox.confirm('确认删除?').then(() => {
          item.children.splice(data.index, 1);
          if (item.children.length == 0) {
            state.cartListData.splice(index,1)
          }
          localStorage.setItem("carData", JSON.stringify(state.cartListData));
        })
      }
    });
  }
}
export default mutations